/*
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is the FxStruts MailReader Application.
 *
 * The Initial Developer of the Original Code is
 * Anirudh Sasikumar (http://anirudhs.chaosnet.org/).
 * Portions created by the Initial Developer are Copyright (C) 2008
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *
*/

package com.adobe.mailreader.view
{
    import com.adobe.mailreader.events.NotifyViewEvent;
    import com.adobe.mailreader.model.MailModelLocator;
    import com.adobe.mailreader.view.components.SubscriptionDetail;
    
    import mx.managers.PopUpManager;
    
    public class SubscriptionDetailMediator extends ViewMediator
    {
        public var view:SubscriptionDetail;
        
        public function SubscriptionDetailMediator()
        {
            super();
            
            notificationInterests = [MailModelLocator.VIEW_SUBSCRIPTION_CREATE_FAILED,
                                     MailModelLocator.VIEW_SUBSCRIPTION_CREATE_SUCCESS,
                                     
                                     MailModelLocator.VIEW_SUBSCRIPTION_EDIT_FAILED,
                                     MailModelLocator.VIEW_SUBSCRIPTION_EDIT_SUCCESS,
                                     
                                     MailModelLocator.VIEW_SUBSCRIPTION_DELETE_SUCCESS,
                                     MailModelLocator.VIEW_SUBSCRIPTION_DELETE_FAILED,
                                     ];
            
        }
        
        
        
        protected override function handleNotification(event:NotifyViewEvent):void
        {
            if ( view == null )
                throw new Error("view cannot be null, please set view property to mxml component");
            
            switch ( event.type )
            {
                case MailModelLocator.VIEW_SUBSCRIPTION_CREATE_FAILED:
                    
                    if ( event.data )
                        view.errBox.showError(event.data);
                    else
                        view.errBox.showError("Unexpected error while deleting user");
                    view.saveButton.enabled = true;
                    
                    break;
                    
                case MailModelLocator.VIEW_SUBSCRIPTION_CREATE_SUCCESS:
                    
                    view.saveButton.enabled = true;
                    view.subVO.token = null;
                    model.mailSubscriptions.addItem(view.subVO);
                    model.mailSubscriptions.refresh();
                    view.subVO = null;
                    view.closeSub();
                    
                    break;
                    
                    
                case MailModelLocator.VIEW_SUBSCRIPTION_EDIT_FAILED:
                    
                    if ( event.data )
                        view.errBox.showError(event.data);
                    else
                        view.errBox.showError("Unexpected error while deleting user");
                    view.saveButton.enabled = true;
                    
                    break;
                    
                case MailModelLocator.VIEW_SUBSCRIPTION_EDIT_SUCCESS:
                    
                    view.saveButton.enabled = true;
                    view.subVO.token = null;
                    model.mailSubscriptions.refresh();
                    view.closeSub();
                    break;
                    
                    
                case MailModelLocator.VIEW_SUBSCRIPTION_DELETE_FAILED:
                    
                    if ( event.data )
                        view.errBox.showError(event.data);
                    else
                        view.errBox.showError("Unexpected error while deleting user");
                    view.saveButton.enabled = true;
                    
                    break;
                    
                case MailModelLocator.VIEW_SUBSCRIPTION_DELETE_SUCCESS:
                    
                    view.saveButton.enabled = true;
                    view.subVO.token = null;
                    view.closeSub();
                    break;
            } 
        }
    }
}